﻿using System.Xml;

namespace ServBus.ListForm2010.Utility
{
    class DmsChoiceType:DmsCommonType
    {
        private DropdownFieldInfo dropdownField;

        private bool mHasChoiceAlready;
        public bool HasChoiceAlready
        {
            get 
            {
                return mHasChoiceAlready;
            }
            set
            {
                mHasChoiceAlready = value;
            }
        }

        public DmsChoiceType(InfoPathContext context, FieldInfo fieldInfo)
            : base(context, fieldInfo)
        {
            this.dropdownField = (DropdownFieldInfo)fieldInfo;
            this.mHasChoiceAlready = mContext.FilesBeSaved.Contains("choices.xml");
        }

        public void BuildFile()
        {
            AddChoiceXsdFile();
            AddChoiceXmlFile();
            if (!HasChoiceAlready)
            {
                AddDropDownFileRefToManifest();
                AddDropdownConnectionToSampleData();
                AddDropdownDataSourceToManifest();
            }
            else
            {
                //sampledata.xml文件中，DataConnection中添加节点
                AddDataConnectionNodeToSampleData();
            }
            
        }

        public void AddChoiceXsdFile()
        {
            string content = string.Format(BaseTypeStrings.DropdownXsdFile, dropdownField.Name, NameSpaceConstants.XSDNameSpaceDefine);
            AddXsdFile("choices.xsd",content,this.HasChoiceAlready);
        }

        public void AddChoiceXmlFile()
        {
            XmlDocument doc = new XmlDocument();
            if (HasChoiceAlready)
            {
                doc.Load(mContext.Path + "choices.xml");
            }
            else
            {
                doc.LoadXml("<root></root>");
            }
            XmlElement root = doc.DocumentElement;
            XmlElement xe = doc.CreateElement(dropdownField.Name);

            foreach (string choice in dropdownField.choicesList)
            {
                XmlElement choiceXe = doc.CreateElement("SharePointListChoice_RW");
                choiceXe.InnerText = choice;
                XmlAttribute xmlAtr = doc.CreateAttribute("DisplayName");
                xmlAtr.Value = choice;
                choiceXe.Attributes.Append(xmlAtr);
                xe.AppendChild(choiceXe);
            }
            root.AppendChild(xe);

            doc.Save(mContext.Path + "choices.xml");
            AddToFileList("choices.xml");
            mContext.AddSavedFile("choices.xml");
        }

        public void AddDropDownFileRefToManifest()
        {
            AddFileListRefToManifest();
        }

        public void AddDropdownConnectionToSampleData()
        {
            string content = string.Format(BaseTypeStrings.DropDownConnectionInSampleData, BaseTypeStrings.DropdownDataSouceName,dropdownField.Name);
            AddConnectionStrToSampleData(content);
        }

        public void AddDropdownDataSourceToManifest()
        {
            string content = string.Format(BaseTypeStrings.DropdownDataObjectInManifest, BaseTypeStrings.DropdownDataSouceName, BaseTypeStrings.DropdownDataSouceName, NameSpaceConstants.XSFNameSpaceDefine);
            AddDataObjectToManifest(content);
        }

        public void AddDataConnectionNodeToSampleData()
        {
            string content = string.Format(BaseTypeStrings.DropdownOneNode, dropdownField.Name);
            AddOneNodeStrToSampledata(content);
        }
    }
}
